#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QHash>
#include <QTextCodec>
#include <QStringListModel>
#include <QThread>
#include <QDebug>

#include "hvtcpserver.h"
#include "log.h"
#include "CustomEvent.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void postEvent();
    void postEvent(const DeviceHeartbeatEvent& event);
    void postEvent(const DisconnectEvent& event);
    void postEvent(const ConnectEvent& event);

private:
    Ui::MainWindow *ui;

    HvTcpServer hvTcpServer;

    // K:imei V:SocketChannelPtr
    QHash<QString, hv::SocketChannelPtr> devices;

    // K:peerAddress V:SocketChannelPtr
    QHash<QString, hv::SocketChannelPtr> channels;

    //K:peerAddress V:imei
    QHash<QString, QString> mapping;

    void channelAdd(const QString& peerAddress, const hv::SocketChannelPtr& channel);
    void channelRemove(const QString& peerAddress);

    void devicesAdd(const QString& peerAddress, const QString &imei, const hv::SocketChannelPtr& channel);
    void devicesRemove(const QString& imei);

protected:
    virtual void customEvent(QEvent* e);
};

extern MainWindow* g_MainWnd;

#endif // MAINWINDOW_H
